package com.ddwl.data.service;


import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.ProtocolConfigEnum;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.data.dao.ProtocolConfigDao;
import com.ddwl.data.model.ProtocolConfig;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.data.ProtocolConfigBo;
import com.ddwl.schema.bo.data.QueryProtocolConfigBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.data.ProtocolConfigVo;
import com.ddwl.schema.vo.data.ProtocolTypeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wenbin
 * @Date 2020/9/17 11:40
 */
@Service
public class ProtocolConfigService {

    @Resource
    private ProtocolConfigDao protocolConfigDao;

    public ProtocolConfigVo getProtocolConfig(QueryProtocolConfigBo bo) {
        bo.setStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        return BeanConverUtils.convertBean(protocolConfigDao.getProtocolConfig(bo), ProtocolConfigVo.class);
    }

    public PageVo<ProtocolConfigVo> page(PageBo<QueryProtocolConfigBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<ProtocolConfig> info = new PageInfo<>(protocolConfigDao.getList(pageBo.getParam()));
        List<ProtocolConfigVo> list = BeanConverUtils.convertBeanList(info.getList(), ProtocolConfigVo.class);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), list);
    }

    /**
     * 获取协议列表
     *
     * @return
     */
    public List<ProtocolTypeVo> getProtocolTypeList() {
        List<ProtocolTypeVo> list = new ArrayList<>();
        for (ProtocolConfigEnum.typeEnum t : ProtocolConfigEnum.typeEnum.values()) {
            ProtocolTypeVo vo = new ProtocolTypeVo();
            vo.setType(t.getType());
            vo.setDesc(t.getDesc());
            list.add(vo);
        }
        return list;
    }

    /**
     * 添加协议
     *
     * @param bo
     */
    @Transactional(rollbackFor = Throwable.class)
    public void create(ProtocolConfigBo bo) {
        ProtocolConfig protocolConfig = BeanConverUtils.convertBean(bo, ProtocolConfig.class);
        protocolConfig.setId(UUIDGenerator.getShortUUID());
        protocolConfigDao.insert(protocolConfig);
    }

    /**
     * 修改协议
     *
     * @param bo
     */
    @Transactional(rollbackFor = Throwable.class)
    public void update(ProtocolConfigBo bo) {
        protocolConfigDao.updateByPrimaryKeySelective(BeanConverUtils.convertBean(bo, ProtocolConfig.class));
    }

    /**
     * 删除协议
     *
     * @param bo
     */
    @Transactional(rollbackFor = Throwable.class)
    public void delete(ProtocolConfigBo bo) {
        protocolConfigDao.deleteById(bo.getId());
    }
}
